use std::env;

///
/// 配置
/// # author
/// - wuxie
/// # version
/// - 2020-9-14
#[derive(Debug,Default)]
pub struct AppConfig {
    data_source: String,
    insert_sql: String,
    insert_value_types: String,
    insert_values: String,
    thread_count: u32,
    batch_count:i32,
    sleep_milli_second: u64
}

impl AppConfig{
    pub fn new()-> AppConfig{
        let data_source= env::var("data_source").expect("DATABASE_URL is not set in .env file");
        let insert_sql = env::var("insert_sql").expect("insert_sql is not set in .env file");
        let insert_values = env::var("insert_values").expect("insert_value_types is not set in .env file");
        let insert_value_types = env::var("insert_value_types").expect("insert_values is not set in .env file");
        let _thread_count = env::var("thread_count").expect("thread_count is not set in .env file");
        let _batch_count = env::var("batch_count").expect("batch_count is not set in .env file");
        let _sleep_milli_second = env::var("sleep_milli_second").expect("sleep_milli_second is not set in .env file");
        AppConfig{
            data_source,
            insert_sql,
            insert_value_types,
            insert_values,
            thread_count: _thread_count.parse().unwrap(),
            batch_count: _batch_count.parse().unwrap(),
            sleep_milli_second: _sleep_milli_second.parse().unwrap()
        }
    }

    pub fn data_source(&self) -> &str{
        return &self.data_source;
    }
    pub fn insert_sql(&self) -> &str{
        return &self.insert_sql;
    }

    pub fn value_types(&self) -> Vec<&str>{
        let types =&self.insert_value_types;
        return types.split(",").collect();
    }
    pub fn values(&self) -> Vec<&str>{
        let values =&self.insert_values;
        return values.split(",").collect();
    }
    pub fn thread_count(&self) ->u32{
        return self.thread_count;
    }
    pub fn batch_count(&self) -> &i32{
        return &self.batch_count;
    }
    pub fn sleep_milli_second(&self) -> &u64{
        return &self.sleep_milli_second;
    }

    ///
    ///打印配置信息
    /// #example
    /// ```
    ///  .print_config();
    /// ```
    ///
    pub fn print_config(&self){
        println!("### config begin-----###");
        println!("# 数据源配置: {:?}", self.data_source());
        println!("# 插入的sql语句: {:?}", self.insert_sql());
        println!("# 插入值类型: {:?}", self.value_types());
        println!("# 插入值: {:?}", self.values());
        println!("# 线程数量: {:?}", self.thread_count());
        println!("# 每批: {:?}条", self.batch_count());
        println!("# 每批延时: {:?}毫秒", self.sleep_milli_second());
        println!("### config end-----###");
    }
}

#[derive(Debug,Default)]
pub struct AboutSoft{
    version : &'static str,
    name : &'static str
}
const ABOUT_SOFT : AboutSoft = AboutSoft{version: "1.0.0.20200911",name: "data-producer-rust"};
impl AboutSoft{
    pub fn print(){
        println!("应用名称:{}",ABOUT_SOFT.name);
        println!("版本:{}",ABOUT_SOFT.version);
    }
}